﻿#include "mainwindowui.h"
#include "ui_mainwindowui.h"

MainWindowUI *g_MainWindow = NULL;

MainWindowUI::MainWindowUI(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowUI)
{
    g_MainWindow = this;
    setFocusPolicy(Qt::StrongFocus);
    ui->setupUi(this);
    setWindowFlags((Qt::FramelessWindowHint));

    init();
}

MainWindowUI::~MainWindowUI()
{
    exit();
    delete ui;
}

void MainWindowUI::init(void){
    getSysScreen();
    retInfo(getCurPath());

    uimStart = 0;
    isShutdown = false;
    isCloseState = false;
    bottomTimer = nullptr;
    canMoveTimer = nullptr;
    pressTimer = nullptr;

    uim = nullptr;
    conf = nullptr;
    myThread = nullptr;

    initConfVar();
    initUimVar();

    resize(conf->confVar.WindowWidth,conf->confVar.WindowHeight);

    initTime();
    initScreen();
    initUimScreen();
}

void MainWindowUI::exit(void){
    initUimDriven(false);
    bool isClose = conf->confVar.WindowClose;
    if(canMoveTimer != nullptr){
        canMoveTimer->stop();
        delete canMoveTimer;
        canMoveTimer = nullptr;
    }
    if(pressTimer != nullptr){
        pressTimer->stop();
        delete pressTimer;
    }
    if(bottomTimer != nullptr){
        bottomTimer->stop();
        delete bottomTimer;
    }
    if(uim != nullptr){
        delete uim;
    }
    if(uim != nullptr){
        delete conf;
    }
    if(isShutdown){
        retInfo("shutdown");
        if(isClose){
            system("shutdown -s -t 00");
        }
    }
}

void MainWindowUI::closeEvent(QCloseEvent *event)
{
    QMessageBox messageBox;

    isCloseState = true;
    messageBox.setText("退出");
    messageBox.setInformativeText("系统退出，请确认?");
    // messageBox.setIcon(QMessageBox::Information);
    messageBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    messageBox.setWindowFlags(messageBox.windowFlags() | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    messageBox.setStyleSheet(QString("QWidget{color: rgb(220, 220, 220);background-color:  rgb(24, 53, 73);} "
                                     "QWidget{margin-right: 60px;} "
                                     "QLabel{font-size: %0px;} "
                                     "QPushButton { width : 200px; height: 120px; font-size: %1px;}"
                                     " %2 %3 %4")
                                 .arg(conf->uiVar.MessageBoxSize).arg(conf->uiVar.MessageBoxSize)
                                 .arg(conf->shellVar.PushButtonShell1)
                                 .arg(conf->shellVar.PushButtonShellHover1)
                                 .arg(conf->shellVar.PushButtonShellPressed1)
                             );
    messageBox.setDefaultButton(QMessageBox::No);

    int ret = messageBox.exec();

    if(ret == QMessageBox::Yes){
        event->accept();
    }
    else{
        isCloseState = false;
        isShutdown = false;
        event->ignore();
    }
}

// 键盘按键事件
void MainWindowUI::keyPressEvent(QKeyEvent *event)
{
    // retInfo(QString("key = %0").arg(event->key()));
    // 普通键
    // switch (event->key())
    // {
    // DEL键
    // case Qt::Key_Delete:
    // break;
    // ESC键
    //        case Qt::Key_Escape:
    //            qDebug() << "Esc";
    //            break;
    // 空格键
    // case Qt::Key_Space:
    // qDebug() << "Space";
    // break;
    // 回车键
    //        case Qt::Key_Return:
    //            qDebug() << "Enter";
    //            break;
    // F1键
    //        case Qt::Key_F1:
    //            qDebug() << "F1";
    //            break;
    // }

    // 两键组合
    // if(event->modifiers() == Qt::ControlModifier){
    // 如果按下了CTRL键
    //        if(event->key() == Qt::Key_Delete){
    //            delImageDir();
    //        }
    // }

    if(event->modifiers() == Qt::AltModifier){
        // 如果按下了ALT键
        if(event->key() == Qt::Key_A){
            qDebug() << "ALT + A";
            on_pushButtonForBackMoveIn_clicked();
            return;
        }
        if(event->key() == Qt::Key_Z){
            qDebug() << "ALT + Z";
            on_pushButtonForBackMoveOut_clicked();
            return;
        }
        if(event->key() == Qt::Key_D){
            qDebug() << "ALT + D";
            on_pushButtonRightFast_clicked();
            return;
        }
        if(event->key() == Qt::Key_C){
            qDebug() << "ALT + C";
            on_pushButtonLeftFast_clicked();
            return;
        }
        if(event->key() == Qt::Key_G){
            qDebug() << "ALT + G";
            photoRightMoveOne();
            return;
        }
        if(event->key() == Qt::Key_B){
            qDebug() << "ALT + B";
            photoLeftMoveOne();
            return;
        }
        if(event->key() == Qt::Key_N){
            qDebug() << "ALT + N";
            photoRightMoveOne(true);
            return;
        }
        if(event->key() == Qt::Key_M){
            qDebug() << "ALT + M";
            photoLeftMoveOne(true);
            return;
        }
        if(event->key() == Qt::Key_X){
            qDebug() << "ALT + X";
            if(!isPush()){
                on_pushButtonPushUp_clicked();
            }else{
                on_pushButtonPushDown_clicked();
            }
            // if(isCloseState){
            // qDebug() << "isCloseState ";
            // QTest::keyPress(this,Qt::Key_Y);
            // }else{
            // on_pushButtonExit_clicked();
            // }
            return;
        }
    }

    // if(event->modifiers() == Qt::ShiftModifier){
    // 如果按下了Shift键
    //        if(event->key() == Qt::Key_M){
    //            qDebug() << "Shift + M";
    //        }
    // }

    // 三键组合Shift + Ctrl + A的实现
    // if(event->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier) && event->key() == Qt::Key_D){
    // qDebug() << "CTRL + Shift + D";
    // if(fsConf->funcVar.Debug){
    //     fsConf->funcVar.Debug = 0;
    // }else{
    //     fsConf->funcVar.Debug = 1;
    // }
    // }
    // if(event->modifiers() == (Qt::AltModifier | Qt::ControlModifier) && event->key() == Qt::Key_D){
    // qDebug() << "CTRL + ALT + D";
    // if(fsConf->funcVar.Debug){
    //     fsConf->funcVar.Debug = 0;
    // }else{
    //     fsConf->funcVar.Debug = 1;
    // }
    // fsConf->saveConfigFuncSave();
    // }
    // if(event->modifiers() == (Qt::AltModifier | Qt::ControlModifier) && event->key() == Qt::Key_L){
    // qDebug() << "CTRL + ALT + L";
    // if(fsConf->funcVar.Log){
    //     fsConf->funcVar.Log = 0;
    // }else{
    //     fsConf->funcVar.Log = 1;
    // }
    // setLog(fsConf->funcVar.Log);
    // fsConf->saveConfigFuncSave();
    // }
}

// 键盘释放事件
void MainWindowUI::keyReleaseEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::AltModifier){
        // 如果ALT键
        qDebug() << " Release " << event->key();
    }
}

void MainWindowUI::initConfVar(void){
    retInfo("init conf");
    if(conf != nullptr){
        delete conf;
        conf = nullptr;
    }
    conf = new Config();
    conf->setConfName(QString("%0/%1.ini").arg(getCurPath()).arg(EXE_MAIN));
    conf->readConfig();
}

void MainWindowUI::initUimVar(void){
    if(uim != nullptr){
        delete myThread;
        delete uim;
        uim = nullptr;
    }
    uim = new UIM(QString("%0/UISimCanFunc.dll").arg(getCurPath()),conf->uimVar.UsbPort,conf->uimVar.ComPort);
    uim->Init();
    retInfo(QString("init uimDll %0").arg(uim->ret));
    myThread = new MyThreadOne(this,uim);
    connect(myThread, SIGNAL(over(bool)),this, SLOT(procUimOver(bool)));
}

void MainWindowUI::initTime(void){
    retInfo("init time");
    isFirstMaxWindow = false;
    bottomTimer = new QTimer(this);
    connect(bottomTimer, SIGNAL(timeout()), this, SLOT(showBottomTimer()));
    bottomTimer->start(1000);
    pressTimer = new QTimer(this);
    connect(pressTimer, SIGNAL(timeout()), this, SLOT(updatePressTimer()));
    isPress = false;
}

void MainWindowUI::initCanMoveTimer(void){
    isKeyCanMove = false;
    if(canMoveTimer == nullptr){
        canMoveTimer = new QTimer(this);
        connect(canMoveTimer, SIGNAL(timeout()), this, SLOT(updateCanMoveTimer()));
        retInfo("init focus auto timer");
    }
}

void MainWindowUI::initScreen(void){
    //设置
    ui->pushButtonSetup->setText(conf->uiVar.PushButtonSetupText);
    ui->pushButtonSetup->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                           .arg(getPushButtonSize(conf->uiVar.PushButtonSetupSize,
                                                                  conf->uiVar.PushButtonSetupWidth,
                                                                  conf->uiVar.PushButtonSetupHeight))
                                           .arg(conf->shellVar.PushButtonShell1)
                                           .arg(conf->shellVar.PushButtonShellHover1)
                                           .arg(conf->shellVar.PushButtonShellPressed1)
                                           .arg(conf->shellVar.PushButtonShellDisabled)
                                       );

    //退出
    ui->pushButtonExit->setText(conf->uiVar.PushButtonExitText);
    ui->pushButtonExit->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                          .arg(getPushButtonSize(conf->uiVar.PushButtonExitSize,
                                                                 conf->uiVar.PushButtonExitWidth,
                                                                 conf->uiVar.PushButtonExitHeight))
                                          .arg(conf->shellVar.PushButtonShell1)
                                          .arg(conf->shellVar.PushButtonShellHover1)
                                          .arg(conf->shellVar.PushButtonShellPressed1)
                                          .arg(conf->shellVar.PushButtonShellDisabled)
                                      );

    //关机
    ui->pushButtonHalt->setText(conf->uiVar.PushButtonHaltText);
    ui->pushButtonHalt->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                          .arg(getPushButtonSize(conf->uiVar.PushButtonHaltSize,
                                                                 conf->uiVar.PushButtonHaltWidth,
                                                                 conf->uiVar.PushButtonHaltHeight))
                                          .arg(conf->shellVar.PushButtonShell1)
                                          .arg(conf->shellVar.PushButtonShellHover1)
                                          .arg(conf->shellVar.PushButtonShellPressed1)
                                          .arg(conf->shellVar.PushButtonShellDisabled)
                                      );
    //装片控制
    ui->labelPosition->setText(conf->uiVar.LabelPositionText);
    ui->labelPosition->setStyleSheet(QString("%0 %1")
                                         .arg(getLabelSize(conf->uiVar.LabelPositionSize,
                                                           conf->uiVar.LabelPositionWidth,
                                                           conf->uiVar.LabelPositionHeight))
                                         .arg(conf->shellVar.LabelShell1)
                                      );
    //出卷复位
    ui->pushButtonForBackReset->setText(conf->uiVar.PushButtonForBackResetText);
    ui->pushButtonForBackReset->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                  .arg(getPushButtonSize(conf->uiVar.PushButtonForBackResetSize,
                                                                         conf->uiVar.PushButtonForBackResetWidth,
                                                                         conf->uiVar.PushButtonForBackResetHeight))
                                                  .arg(conf->shellVar.PushButtonShell2)
                                                  .arg(conf->shellVar.PushButtonShellHover2)
                                                  .arg(conf->shellVar.PushButtonShellPressed2)
                                                  .arg(conf->shellVar.PushButtonShellDisabled)
                                              );
    //居中
    ui->pushButtonForBackMoveIn->setText(conf->uiVar.PushButtonForBackMoveInText);
    ui->pushButtonForBackMoveIn->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                   .arg(getPushButtonSize(conf->uiVar.PushButtonForBackMoveInSize,
                                                                          conf->uiVar.PushButtonForBackMoveInWidth,
                                                                          conf->uiVar.PushButtonForBackMoveInHeight))
                                                   .arg(conf->shellVar.PushButtonShell3)
                                                   .arg(conf->shellVar.PushButtonShellHover2)
                                                   .arg(conf->shellVar.PushButtonShellPressed3)
                                                   .arg(conf->shellVar.PushButtonShellDisabled)
                                               );
    //出卷
    ui->pushButtonForBackMoveOut->setText(conf->uiVar.PushButtonForBackMoveOutText);
    ui->pushButtonForBackMoveOut->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                    .arg(getPushButtonSize(conf->uiVar.PushButtonForBackMoveOutSize,
                                                                           conf->uiVar.PushButtonForBackMoveOutWidth,
                                                                           conf->uiVar.PushButtonForBackMoveOutHeight))
                                                    .arg(conf->shellVar.PushButtonShell3)
                                                    .arg(conf->shellVar.PushButtonShellHover2)
                                                    .arg(conf->shellVar.PushButtonShellPressed3)
                                                    .arg(conf->shellVar.PushButtonShellDisabled)
                                                );
    //影像向上
    ui->pushButtonForBackUp->setText(conf->uiVar.PushButtonForBackUpText);
    ui->pushButtonForBackUp->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                               .arg(getPushButtonSize(conf->uiVar.PushButtonForBackUpSize,
                                                                      conf->uiVar.PushButtonForBackUpWidth,
                                                                      conf->uiVar.PushButtonForBackUpHeight))
                                               .arg(conf->shellVar.PushButtonShell3)
                                               .arg(conf->shellVar.PushButtonShellHover2)
                                               .arg(conf->shellVar.PushButtonShellPressed3)
                                               .arg(conf->shellVar.PushButtonShellDisabled)
                                           );
    //影像向下
    ui->pushButtonForBackDown->setText(conf->uiVar.PushButtonForBackDownText);
    ui->pushButtonForBackDown->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                                 .arg(getPushButtonSize(conf->uiVar.PushButtonForBackDownSize,
                                                                        conf->uiVar.PushButtonForBackDownWidth,
                                                                        conf->uiVar.PushButtonForBackDownHeight))
                                                 .arg(conf->shellVar.PushButtonShell3)
                                                 .arg(conf->shellVar.PushButtonShellHover2)
                                                 .arg(conf->shellVar.PushButtonShellPressed3)
                                                 .arg(conf->shellVar.PushButtonShellDisabled)
                                             );
    //压片复位
    ui->pushButtonPushReset->setText(conf->uiVar.PushButtonPushMoveResetText);
    ui->pushButtonPushReset->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                               .arg(getPushButtonSize(conf->uiVar.PushButtonPushMoveResetSize,
                                                                      conf->uiVar.PushButtonPushMoveResetWidth,
                                                                      conf->uiVar.PushButtonPushMoveResetHeight))
                                               .arg(conf->shellVar.PushButtonShell2)
                                               .arg(conf->shellVar.PushButtonShellHover2)
                                               .arg(conf->shellVar.PushButtonShellPressed2)
                                               .arg(conf->shellVar.PushButtonShellDisabled)
                                           );
    //压片
    ui->pushButtonPushDown->setText(conf->uiVar.PushButtonPushMoveDownText);
    ui->pushButtonPushDown->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                              .arg(getPushButtonSize(conf->uiVar.PushButtonPushMoveDownSize,
                                                                     conf->uiVar.PushButtonPushMoveDownWidth,
                                                                     conf->uiVar.PushButtonPushMoveDownHeight))
                                              .arg(conf->shellVar.PushButtonShell3)
                                              .arg(conf->shellVar.PushButtonShellHover2)
                                              .arg(conf->shellVar.PushButtonShellPressed3)
                                              .arg(conf->shellVar.PushButtonShellDisabled)
                                          );
    //松下
    ui->pushButtonPushUp->setText(conf->uiVar.PushButtonPushMoveUpText);
    ui->pushButtonPushUp->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                            .arg(getPushButtonSize(conf->uiVar.PushButtonPushMoveUpSize,
                                                                   conf->uiVar.PushButtonPushMoveUpWidth,
                                                                   conf->uiVar.PushButtonPushMoveUpHeight))
                                            .arg(conf->shellVar.PushButtonShell3)
                                            .arg(conf->shellVar.PushButtonShellHover2)
                                            .arg(conf->shellVar.PushButtonShellPressed3)
                                            .arg(conf->shellVar.PushButtonShellDisabled)
                                        );
    //走片控制
    ui->labelMove->setText(conf->uiVar.LabelMoveText);
    ui->labelMove->setStyleSheet(QString("%0 %1")
                                     .arg(getLabelSize(conf->uiVar.LabelMoveSize,
                                                       conf->uiVar.LabelMoveWidth,
                                                       conf->uiVar.LabelMoveHeight))
                                     .arg(conf->shellVar.LabelShell1)
                                     );
    //快左
    ui->pushButtonLeftFast->setText(conf->uiVar.PushButtonLeftFastText);
    ui->pushButtonLeftFast->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                              .arg(getPushButtonSize(conf->uiVar.PushButtonLeftFastSize,
                                                                     conf->uiVar.PushButtonLeftFastWidth,
                                                                     conf->uiVar.PushButtonLeftFastHeight))
                                              .arg(conf->shellVar.PushButtonShell3)
                                              .arg(conf->shellVar.PushButtonShellHover2)
                                              .arg(conf->shellVar.PushButtonShellPressed3)
                                              .arg(conf->shellVar.PushButtonShellDisabled)
                                           );
    //快右
    ui->pushButtonRightFast->setText(conf->uiVar.PushButtonRightFastText);
    ui->pushButtonRightFast->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                               .arg(getPushButtonSize(conf->uiVar.PushButtonRightFastSize,
                                                                     conf->uiVar.PushButtonRightFastWidth,
                                                                     conf->uiVar.PushButtonRightFastHeight))
                                               .arg(conf->shellVar.PushButtonShell3)
                                               .arg(conf->shellVar.PushButtonShellHover2)
                                               .arg(conf->shellVar.PushButtonShellPressed3)
                                               .arg(conf->shellVar.PushButtonShellDisabled)
                                          );
    //向左
    ui->pushButtonLeftMove->setText(conf->uiVar.PushButtonLeftMoveText);
    ui->pushButtonLeftMove->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                              .arg(getPushButtonSize(conf->uiVar.PushButtonLeftMoveSize,
                                                                     conf->uiVar.PushButtonLeftMoveWidth,
                                                                     conf->uiVar.PushButtonLeftMoveHeight))
                                              .arg(conf->shellVar.PushButtonShell3)
                                              .arg(conf->shellVar.PushButtonShellHover2)
                                              .arg(conf->shellVar.PushButtonShellPressed3)
                                              .arg(conf->shellVar.PushButtonShellDisabled)
                                          );
    //向右
    ui->pushButtonRightMove->setText(conf->uiVar.PushButtonRightMoveText);
    ui->pushButtonRightMove->setStyleSheet(QString("%0 %1 %2 %3 %4")
                                               .arg(getPushButtonSize(conf->uiVar.PushButtonRightMoveSize,
                                                                      conf->uiVar.PushButtonRightMoveWidth,
                                                                      conf->uiVar.PushButtonRightMoveHeight))
                                               .arg(conf->shellVar.PushButtonShell3)
                                               .arg(conf->shellVar.PushButtonShellHover2)
                                               .arg(conf->shellVar.PushButtonShellPressed3)
                                               .arg(conf->shellVar.PushButtonShellDisabled)
                                           );

    ui->labelVer->setText(QString("V%0").arg(EXE_VERSION));
    // setWindowTitle(QString("%0 Ver[%1]").arg(conf->confVar.Title).arg(EXE_VERSION));
    // setWindowIcon(QIcon(":/img/logo.ico"));

    // showBottomLeftInfo();
    // showBottomRightInfo();
    // showBottomForBackInfo();
    // showBottomPushInfo();
    // showBottomRateInfo();
}

void MainWindowUI::initUimScreen(void){
    // ui->pushButtonExit->setEnabled((uimStart));
    // ui->pushButtonHalt->setEnabled((uimStart));

    ui->pushButtonForBackReset->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isForBackId());
    ui->pushButtonForBackMoveIn->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isForBackId());
    ui->pushButtonForBackMoveOut->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isForBackId());

    ui->pushButtonForBackUp->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isForBackId());
    ui->pushButtonForBackDown->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isForBackId());

    ui->pushButtonPushDown->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isPushId());
    ui->pushButtonPushUp->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isPushId());
    ui->pushButtonPushReset->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isPushId());

    ui->pushButtonLeftFast->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isLeftId());
    ui->pushButtonLeftMove->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isLeftId());
    ui->pushButtonRightFast->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isRightId());
    ui->pushButtonRightMove->setEnabled((uimStart==2)&&uim->isOpen()&&uim->isRightId());
}

void MainWindowUI::procUimOver(bool ok){
    if(ok){
        // showBottomUimState(QString("%0,%1,%2,%3").arg(uim->getLeftId()).arg(uim->getRightId()).arg(uim->getForBackId()).arg(uim->getPushId()));
        initCanMoveTimer();
        //卷片
        // showBottomUimLeftPos();
        // showBottomUimRightPos();
        // showBottomUimForBackPos();
        // showBottomUimPushPos();

        uim->canLeftOff(true);
        uim->canRightOff(true);

        if(conf->uimVar.CanForBackEna){
            uim->canForBackEna(true);
        }else{
            uim->canForBackOff(true);
        }
        if(conf->uimVar.CanPushEna){
            uim->canPushEna(true);
        }else{
            uim->canPushOff(true);
        }
        //回调
        canCallback();
        uimStart = 2;
        photoMove = 0;
        curMove = 0;
        curForbackReset = 0;
        curPushReset = 0;

        //启动成功后:操作
        //压片电机复位
        //进出电机复位
        //压片上抬
        on_pushButtonPushUp_clicked();
    }else{
        // showBottomUimState("启动失败");
        uimStart = -5;
    }
    initUimScreen();
}

//是否下松
bool MainWindowUI::isPush(void){
    if(!uim->isPushId()){
        return true;
    }
    if(uim->getPushPos()<=conf->uimVar.CanPushLimit){
        //上压
        return false;
    }
    return true;
}

void MainWindowUI::initUimDriven(bool open){
    if(open){
        //打开
        if(uim->isOpen()){
            // 已经打开
            return;
        }
        retInfo(QString("initUimDriven %0 %1").arg(uim->isOpen()).arg(open));
        // showBottomUimState("启动电机");
        uimStart = 1;
        myThread->start();
    }else{
        //关闭
        if(!uim->isOpen()){
            //已经关闭
            return;
        }
        //关闭
        retInfo(QString("initUimDriven %0 %1").arg(uim->isOpen()).arg(open));
        uim->canLeftOff(true);
        uim->canRightOff(true);
        uim->canForBackOff(true);
        uim->canPushOff(true);
        uim->canCloseGateWay();
        photoMove = 0;
        curMove = 0;
        QObject::disconnect(this, SIGNAL(SendRTCNMsg(QString,int,int)), this, SLOT(retCanMsg(QString,int,int)));
    }
}

void MainWindowUI::showBottomTimer(void){
    while(true){
        if(!isFirstMaxWindow){
            isFirstMaxWindow = true;
            if(!this->isMaximized()&&conf->confVar.WindowMax){
                setWindowState(Qt::WindowMaximized);
            }
            uimStart = 0;
            break;
        }
        if(uimStart == 0){
            initUimDriven(true);
        }
        if(uimStart < 0){
            uimStart++;
        }
        break;
    }

    QDateTime time = QDateTime::currentDateTime();
    // ui->labelTime->setText(time.toString(" yyyy-MM-dd hh:mm:ss dddd"));
    ui->labelTime->setText(time.toString("hh:mm"));
    ui->labelDay->setText(time.toString("MM月dd日 dddd"));
}

//电机移动定时器：动态显示当前位置，判断是否超时或
void MainWindowUI::updateCanMoveTimer(void){
    qint64 ms = QDateTime::currentMSecsSinceEpoch()-curCanMoveTimer;
    bool isMove = false;

    if(photoMove){
        retInfo(QString("PhotoMove[%0] %1 %2 %3 qec = %4 %5")
                    .arg(photoMove)
                    .arg(uim->getCanLeftMove())
                    .arg(uim->getCanRightMove())
                    .arg(ms).arg(curCanMoveQec).arg(uim->getLeftQec()));
        if(uim->getCanLeftMove() || uim->getCanLeftMove()){
            isMove = true;
        }
    }else{
        if(curMove == 1){
            retInfo(QString("CanMove %0 %1")
                        .arg(uim->getCanForBackMove())
                        .arg(ms));
        }
        if(curMove == 2){
            retInfo(QString("CanMove %0 %1")
                        .arg(uim->getCanPushMove())
                        .arg(ms));
        }
    }
    while(true){
        if(photoMove){
            // showBottomUimLeftPos();
            // showBottomUimRightPos();
            if(photoMove==1 || photoMove==4){
                //判断编码
                int qec = uim->getLeftQec();
                if(abs(curCanMoveQec-qec)==0){
                    //停止
                    photoStopMove();
                    break;
                }
                curCanMoveQec = qec;
            }
            break;
        }
        if(curMove == 1){
            // showBottomUimForBackPos();
            if(uim->getCanForBackMove()!=0){
                //显示位置
                isMove = true;
            }
        }
        if(curMove == 2){
            // showBottomUimPushPos();
            if(uim->getCanPushMove()!=0){
                //显示位置
                isMove = true;
            }
        }
        break;
    }
    if(!isMove){
        canMoveTimer->stop();
    }
}

void MainWindowUI::canMoveTimerStart(bool en){
    if(en){
        canMoveTimer->start(conf->uimVar.CanTimer);
        curCanMoveTimer = QDateTime::currentMSecsSinceEpoch();
        curCanMoveQec = uim->getLeftQec();
    }else{
        canMoveTimer->stop();
        curCanMoveTimer = 0;
    }
}

//设置
void MainWindowUI::on_pushButtonSetup_clicked()
{
    setup *set = new setup(nullptr, conf);
    set->setWindowModality(Qt::ApplicationModal);

    connect(set, SIGNAL(setupSave()),this,SLOT(recvSetupSave()));
    set->show();
}

void MainWindowUI::recvSetupSave(){
    // showBottomLeftInfo();
    // showBottomRightInfo();
    // showBottomRateInfo();
}

//退出
void MainWindowUI::on_pushButtonExit_clicked()
{
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    close();
}

//关机
void MainWindowUI::on_pushButtonHalt_clicked()
{
    isShutdown = true;
    on_pushButtonExit_clicked();
}

//复位
void MainWindowUI::on_pushButtonForBackReset_clicked()
{
    if(!uim->isForBackId()){
        return;
    }

    //压片上抬
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    if(curForbackReset){
        //停止
        canStopMove();
        return;
    }
    curForbackReset = 1;
    uim->canForBackEna(true);
    uim->canSetSTP(uim->getForBackId(),0);
    uim->canSetSPD(uim->getForBackId(),-conf->uimVar.CanForBackResetRate);
}


//移动IN
void MainWindowUI::on_pushButtonForBackMoveIn_clicked()
{
    if(!uim->isForBackId()){
        return;
    }
    //压片上抬
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    if(uim->getCanForBackMove()){
        //停止
        canStopMove();
        return;
    }
    curForbackReset = 0;
    canMovePos(uim->getForBackId(),conf->uiVar.PushButtonForBackMoveInText,conf->uimVar.CanForBackSetIn,conf->uimVar.CanForBackFastRate,-1);
}

//移动OUT
void MainWindowUI::on_pushButtonForBackMoveOut_clicked()
{
    if(!uim->isForBackId()){
        return;
    }
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    if(uim->getCanForBackMove()){
        //停止
        canStopMove();
        return;
    }
    curForbackReset = 0;
    canMovePos(uim->getForBackId(),conf->uiVar.PushButtonForBackMoveOutText,conf->uimVar.CanForBackSetOut,conf->uimVar.CanForBackFastRate,1);
}

//上-单步
void MainWindowUI::on_pushButtonForBackUp_clicked()
{
    if(!uim->isForBackId()){
        return;
    }
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    if(uim->getCanForBackMove()){
        //停止
        canStopMove();
        return;
    }
    curForbackReset = 0;
    canMove("前Up单步",0,-1);
}

//上-按住
// void MainWindowUI::on_pushButtonForBackUp_pressed()
// {
//     if(!uim->isForBackId()){
//         return;
//     }
//     if(!isPush()){
//         on_pushButtonPushUp_clicked();
//         return;
//     }
//     curForbackReset = 0;
//     canMove("前IN连续",1,-1);
// }

//上-释放
// void MainWindowUI::on_pushButtonForBackUp_released()
// {
//     if(!uim->isForBackId()){
//         return;
//     }
//     canStopMove();
// }

//下-单步
void MainWindowUI::on_pushButtonForBackDown_clicked()
{
    if(!uim->isForBackId()){
        return;
    }
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    if(uim->getCanForBackMove()){
        //停止
        canStopMove();
        return;
    }
    curForbackReset = 0;
    canMove("前Down单步",0,1);
}

//下-按住
// void MainWindowUI::on_pushButtonForBackDown_pressed()
// {
//     if(!uim->isForBackId()){
//         return;
//     }
//     if(!isPush()){
//         on_pushButtonPushUp_clicked();
//         return;
//     }
//     curForbackReset = 0;
//     canMove("前OUT连续",1,1);
// }

//下-释放
// void MainWindowUI::on_pushButtonForBackDown_released()
// {
//     if(!uim->isForBackId()){
//         return;
//     }
//     canStopMove();
// }

//压片-下压
void MainWindowUI::on_pushButtonPushDown_clicked()
{
    if(!uim->isPushId()){
        return;
    }
    curPushReset = 0;
    canMovePos(uim->getPushId(),conf->uiVar.PushButtonPushMoveDownText,conf->uimVar.CanPushDown,conf->uimVar.CanPushRate,-1);
}

//压片-上抬
void MainWindowUI::on_pushButtonPushUp_clicked()
{
    if(!uim->isPushId()){
        return;
    }
    curPushReset = 0;
    canMovePos(uim->getPushId(),conf->uiVar.PushButtonPushMoveUpText,conf->uimVar.CanPushUp,conf->uimVar.CanPushRate,1);
}

//压片-复位
void MainWindowUI::on_pushButtonPushReset_clicked()
{
    if(!uim->isPushId()){
        return;
    }
    curPushReset = 1;
    uim->canForBackEna(true);
    uim->canSetSTP(uim->getPushId(),0);
    uim->canSetSPD(uim->getPushId(),-conf->uimVar.CanPushRate);
}

//胶片按钮显示
void MainWindowUI::photoScreenMove(void){
    //photoMove;    //0-无，1-快左，2-向左一步，3-向左连续，4-向右连续，5-向右一步，6-快右
    QString s="停止";
    switch(photoMove){
    case 1:
        ui->pushButtonLeftFast->setText(s);
        ui->pushButtonLeftMove->setEnabled(false);
        ui->pushButtonRightMove->setEnabled(false);
        ui->pushButtonRightFast->setEnabled(false);
        break;
    case 2:
        break;
    case 3:
        break;
    case 4:
        break;
    case 5:
        break;
    case 6:
        ui->pushButtonRightFast->setText(s);
        ui->pushButtonLeftFast->setEnabled(false);
        ui->pushButtonLeftMove->setEnabled(false);
        ui->pushButtonRightMove->setEnabled(false);
        break;
    default:
        initUimScreen();
        break;
    }
}

//胶片停止
void MainWindowUI::photoStopMove(void){
    // showBottomUimState("停止走片");
    retInfo(QString("photoStopMove %0").arg(photoMove));
    if(photoMove){
        // if(photoMove>=4){
        uim->canSetSTP(uim->getRightId(),0);
        // }else{
        uim->canSetSTP(uim->getLeftId(),0);
        ui->pushButtonLeftFast->setText(conf->uiVar.PushButtonLeftFastText);
        ui->pushButtonRightFast->setText(conf->uiVar.PushButtonRightFastText);
        // }
    }
    uim->canLeftOff(conf->uimVar.CanLeftOff);
    uim->canRightOff(conf->uimVar.CanRightOff);
    // showBottomUimLeftPos();
    // showBottomUimRightPos();
    uim->setCanLeftMove();
    uim->setCanRightMove();
    photoMove = 0;
    photoScreenMove();
    canMoveTimerStart();
}

//快左
void MainWindowUI::on_pushButtonLeftFast_clicked()
{
    if(!uim->isLeftId()){
        return;
    }
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    retInfo(QString("on_pushButtonLeftFast_clicked %0").arg(photoMove));
    if(photoMove){
        photoStopMove();
        return;
    }
    uim->canRightOff();
    // showBottomUimState("快左");
    // showBottomUimLeftPos();
    uim->canLeftEna();
    uim->canSetSPD(uim->getLeftId(),conf->uimVar.CanLeftFastRate*conf->uimVar.CanLeftEncodeState);
    uim->setCanLeftMove(true);
    uim->setCanRightMove();
    photoMove = 1;
    photoScreenMove();
    canMoveTimerStart(true);
}

//快右
void MainWindowUI::on_pushButtonRightFast_clicked()
{
    if(!uim->isRightId()){
        return;
    }
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    retInfo(QString("on_pushButtonRightFast_clicked %0").arg(photoMove));
    if(photoMove){
        photoStopMove();
        return;
    }
    uim->canLeftOff();
    // showBottomUimState("快右");
    // showBottomUimRightPos();
    uim->canRightEna();
    uim->canSetSPD(uim->getRightId(),conf->uimVar.CanLeftFastRate*conf->uimVar.CanRightEncodeState);
    uim->setCanLeftMove();
    uim->setCanRightMove(true);
    photoMove = 6;
    photoScreenMove();
    canMoveTimerStart(true);
}

//向左一步
void MainWindowUI::photoLeftMoveOne(bool isSlow){
    if(!uim->isLeftId())return;
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    retInfo("photoLeftMoveOne");
    if(photoMove){
        photoStopMove();
        return;
    }
    uim->canRightOff();
    // showBottomUimState("向左一步");
    if(conf->uimVar.CanLeftOrg){
        uim->canSetOrg(uim->getLeftId());
    }
    // showBottomUimLeftPos();
    uim->canLeftEna();
    uim->canSetSPD(uim->getLeftId(),conf->uimVar.CanLeftRate);
    if(isSlow){
        uim->canSetSTP(uim->getLeftId(),conf->uimVar.CanLeftSlowStep*conf->uimVar.CanLeftEncodeState);
    }else{
        uim->canSetSTP(uim->getLeftId(),conf->uimVar.CanLeftStep*conf->uimVar.CanLeftEncodeState);
    }
    photoMove = 3;
}

//向右一步
void MainWindowUI::photoRightMoveOne(bool isSlow){
    if(!uim->isRightId())return;
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    retInfo("photoRightMoveOne");
    if(photoMove){
        photoStopMove();
        return;
    }
    uim->canLeftOff();
    // showBottomUimState("向右一步");
    if(conf->uimVar.CanRightOrg){
        uim->canSetOrg(uim->getRightId());
    }
    // showBottomUimRightPos();
    uim->canRightEna();
    uim->canSetSPD(uim->getRightId(),conf->uimVar.CanRightRate);
    if(isSlow){
        uim->canSetSTP(uim->getRightId(),conf->uimVar.CanRightSlowStep*conf->uimVar.CanRightEncodeState);
    }else{
        uim->canSetSTP(uim->getRightId(),conf->uimVar.CanRightStep*conf->uimVar.CanRightEncodeState);
    }
    photoMove = 4;
}

//向左连续
void MainWindowUI::photoLeftMove(void){
    if(!uim->isLeftId())return;
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    retInfo("photoLeftMove");
    uim->canRightOff();
    // showBottomUimState("向左");
    // showBottomUimLeftPos();
    uim->canLeftEna();
    uim->canSetSTP(uim->getLeftId(),0);
    uim->canSetSPD(uim->getLeftId(),conf->uimVar.CanLeftRate);
    uim->setCanLeftMove(true);
    uim->setCanRightMove();
    photoMove = 3;
    photoScreenMove();
    canMoveTimerStart(true);
}

//向右连续
void MainWindowUI::photoRightMove(void){
    if(!uim->isRightId())return;
    if(!isPush()){
        on_pushButtonPushUp_clicked();
        return;
    }
    retInfo("photoRightMove");
    uim->canLeftOff();
    // showBottomUimState("向右");
    // showBottomUimRightPos();
    uim->canRightEna();
    uim->canSetSTP(uim->getRightId(),0);
    uim->canSetSPD(uim->getRightId(),conf->uimVar.CanRightRate);
    uim->setCanLeftMove();
    uim->setCanRightMove(true);
    photoMove = 5;
    photoScreenMove();
    canMoveTimerStart(true);
}

//按钮超时
void MainWindowUI::updatePressTimer(void){
    pressTimer->stop();
    if(isPress){
        //按住
        if(curPressMode==1){
            photoLeftMove();
            return;
        }
        if(curPressMode==2){
            photoRightMove();
            return;
        }
    }else{
        //释放
        if(curPressMode){
            photoStopMove();
            curPressMode = 0;
        }
    }
}

//向左
void MainWindowUI::on_pushButtonLeftMove_clicked()
{
    retInfo(QString("向左 %0 %1").arg(pressTimer->isActive()).arg(curPressMode));
    if(pressTimer->isActive()){
        if(curPressMode==0){
            photoLeftMoveOne();
        }
    }
}

//向右
void MainWindowUI::on_pushButtonRightMove_clicked()
{
    retInfo(QString("向右 %0 %1").arg(pressTimer->isActive()).arg(curPressMode));
    if(pressTimer->isActive()){
        if(curPressMode==0){
            photoRightMoveOne();
        }
    }
}

//左-按住
void MainWindowUI::on_pushButtonLeftMove_pressed()
{
    retInfo("向左press");
    pressTimer->start(conf->uiVar.PushButtonPressTimer);
    isPress = true;
    curPressMode = 1;
}

//左-释放
void MainWindowUI::on_pushButtonLeftMove_released()
{
    retInfo("向左release");
    isPress = false;
    if(pressTimer->isActive()){
        curPressMode = 0;
        return;
    }
    pressTimer->start(conf->uiVar.PushButtonPressTimer);
}

//右-按住
void MainWindowUI::on_pushButtonRightMove_pressed()
{
    retInfo("向右press");
    pressTimer->start(conf->uiVar.PushButtonPressTimer);
    isPress = true;
    curPressMode = 2;
}

//右-释放
void MainWindowUI::on_pushButtonRightMove_released()
{
    retInfo("向右release");
    isPress = false;
    if(pressTimer->isActive()){
        curPressMode = 0;
        return;
    }
    pressTimer->start(conf->uiVar.PushButtonPressTimer);
}

// CAN 函数
// 回调信息
void MainWindowUI::canCallback(void){
    retInfo(QString("can[%0] Callback").arg(uim->getIndex()));
    QObject::connect(this, SIGNAL(SendRTCNMsg(QString,int,int)), this, SLOT(retCanMsg(QString,int,int)));
    UIMRegRtcnCallBack(0,uim->getIndex(),MainWindowUI::StaticRcvRtcnNotify);
}

//处理电机返回信息
void MainWindowUI::retCanMsg(QString msg,int code,int node){
    // emit msg
    //code:113/241-S1传感1通知,114/242-S2传感2通知,115/243-S3传感3通知,117/245-STP到达通知,118-ORG零点通知
    if(uim->isLeftId(node)&&isStepMove(code)){
        retInfo(QString("left move %0 [%1 %2 %3]").arg(photoMove).arg(msg).arg(code).arg(node));
        uim->canLeftOff(conf->uimVar.CanLeftOff);
        // showBottomUimLeftPos();
        return;
    }
    if(uim->isRightId(node)&&isStepMove(code)){
        retInfo(QString("right move %0 [%1 %2 %3]").arg(photoMove).arg(msg).arg(code).arg(node));
        uim->canRightOff(conf->uimVar.CanRightOff);
        // showBottomUimRightPos();
        return;
    }
    retInfo(msg);
    if(uim->isForBackId(node)){
        //前后
        procCanForBackSense(code);
        return;
    }
    if(uim->isPushId(node)){
        //压片
        procCanPushSense(code);
        return;
    }
}

//回调函数
void MainWindowUI::StaticRcvRtcnNotify(unsigned int dwDevIndex, P_CAN_MSG_OBJ pRtcnMsgObj, unsigned int dwMsgLen)
{
    g_MainWindow->RcvRtcnNotify(dwDevIndex,pRtcnMsgObj,dwMsgLen);
}

void MainWindowUI::RcvRtcnNotify(unsigned int dwDevIndex, P_CAN_MSG_OBJ pRtcnMsgObj, unsigned int dwMsgLen)
{
    Q_UNUSED(dwMsgLen);
    unsigned int dwCanNodeId = ((pRtcnMsgObj->ID>>3)&0x060)+((pRtcnMsgObj->ID>>19)&0x01F);
    unsigned int iRCW = pRtcnMsgObj->ID&0x0FF;
    //    unsigned int iData = pRtcnMsgObj->Data[0]&0x0FF;
    QString strRTCNType = QString();

    switch (iRCW)
    {
    case 62:
    {
        strRTCNType = QString::fromLocal8Bit("%0 %1 %2 %3 %4").arg("UID900").arg(pRtcnMsgObj->Data[0]).arg(pRtcnMsgObj->Data[1]).arg(pRtcnMsgObj->Data[2]).arg(pRtcnMsgObj->Data[3]);
    }
    break;
    case 113:
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:S1->下降沿");
    }
    break;
    case 113 + (1<<7):
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:S1->上升沿");
    }
    break;
    case 114:
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:S2->下降沿");
    }
    break;
    case 114 + (1<<7):
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:S2->上升沿");
    }
    break;
    // case 115:
    // {
    //     strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:S3->下降沿");
    // }
    // break;
    // case 115 + (1<<7):
    // {
    //     strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:S3->上升沿");
    // }
    // break;
    // case 116:
    // {
    //    strRTCNType = QString::fromLocal8Bit("%0 %1").arg("UIM242:P4").arg("IDS_FAILING_RTCN");
    // }
    // break;
    // case 116 + (1<<7):
    // {
    //    strRTCNType = QString::fromLocal8Bit("%0 %1").arg("UIM242:P4").arg("IDS_RISING_RTCN");
    // }
    // break;
    case 117:
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:STP");
    }
    break;
    case 117 + (1<<7):
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:STP");
    }
    break;
    case 118:
    {
        strRTCNType = QString::fromLocal8Bit("%0").arg("UIM242:ORG");
    }
    break;
        //        case 119:
        //        {
        //            strRTCNType = QString::fromLocal8Bit("%0 %1").arg("UIM242:").arg("IDS_SQT_NOTIFY");
        //        }
        //        break;
        //        case 112:
        //        {
        //            strRTCNType = QString::fromLocal8Bit("UID820/828:CH %0 %1").arg(iData).arg("IDS_FAILING_RTCN");
        //        }
        //        break;
        //        case 112 + (1<<7):
        //        {
        //            strRTCNType = QString::fromLocal8Bit("UID820/828:CH %0 %1" ).arg(iData).arg("IDS_RISING_RTCN");
        //        }
        //        break;
    case 90:
    {
        if( pRtcnMsgObj->Data[0]%2 == 0)
        {
            strRTCNType = QString::fromLocal8Bit("P%0L").arg((pRtcnMsgObj->Data[0]+1)/2);
        }
        else
        {
            strRTCNType = QString::fromLocal8Bit("P%0H").arg((pRtcnMsgObj->Data[0]+1)/2);
        }
    }
    default:
        break;
    }
    strRTCNType += QString(" retCode=%0 nodeId=%1 devIndex=%2").arg(iRCW).arg(dwCanNodeId).arg(dwDevIndex);
    // retInfo(strRTCNType);
    emit SendRTCNMsg(strRTCNType,iRCW,dwCanNodeId);
}

void MainWindowUI::canMovePos(const int id,const QString msg,const int locate,int rate,const int direction){
    // showBottomUimState(msg);
    isKeyCanMove=false;
    bool isId = 0,isDirect = 0;
    int node=0;
    curMove = 0;
    if(id == conf->uimVar.CanForBackId){
        isId = uim->isForBackId();
        node = uim->getForBackId();
        isDirect = uim->getCanForBackMove();
        curMove = 1;
    }
    if(id == conf->uimVar.CanPushId){
        isId = uim->isPushId();
        node = uim->getPushId();
        isDirect = uim->getCanPushMove();
        curMove = 2;
    }

    retInfo(QString("%0 %1 %2 %3 %4 %5").arg(msg).arg(locate).arg(rate).arg(isId).arg(isDirect).arg(direction));
    //ForBack
    if(!isId)return;
    if(isDirect!=0)return;
    //移动
    uim->canSetSPD(node,rate);
    uim->canSetPOS(node,locate);
    if(id == conf->uimVar.CanForBackId){
        uim->canForBackEna();
    }
    if(id == conf->uimVar.CanPushId){
        uim->canPushEna();
    }
    canMoveTimerStart(true);
    if(curMove == 1){
        // showBottomUimForBackPos();
    }
    if(curMove == 2){
        // showBottomUimPushPos();
    }
}

// bool MainWindowUI::getCanForBackLimitPos(const int direction){
//     int nDirection = direction;
//     int pos = uim->getForBackPos();
//     if(!nDirection){
//         nDirection = uim->getCanForBackMove();
//     }
//     retInfo(QString("getCanForBackLimitPos %0 %1 limit %2 %3").arg(nDirection).arg(pos)
//                 .arg(conf->uimVar.CanForBackLimitIn).arg(conf->uimVar.CanForBackLimitOut));
//     if(nDirection > 0){
//         if((conf->uimVar.CanForBackLimitOut>0)
//             &&(pos>(conf->uimVar.CanForBackLimitOut))){
//             retInfo(QString("ForBack %0 limit far").arg(pos));
//             return true;
//         }
//     }
//     if(nDirection < 0){
//         if((conf->uimVar.CanForBackLimitIn>0)
//             &&(pos<conf->uimVar.CanForBackLimitIn)){
//             retInfo(QString("ForBack %0 limit org").arg(pos));
//             return true;
//         }
//     }
//     return false;
// }

void MainWindowUI::canMove(const QString msg,const bool mode,const int direction){
    // showBottomUimState(msg);
    bool isLimit = false;
    int rate=0;
    isKeyCanMove=mode;

    //ForBack
    if((bool)conf->uimVar.CanForBackMode!=mode)return;
    if(!uim->isForBackId())return;
    if(uim->getCanForBackMove()!=0)return;
    //判断限制位置
    int pos = uim->getForBackPos();
    int step = conf->uimVar.CanForBackStep;
    if(direction<0){
        //UP
        if((conf->uimVar.CanForBackLimitMin>0)
            &&(pos<=conf->uimVar.CanForBackLimitMin)){
            return;
        }
        if(step>(pos-conf->uimVar.CanForBackLimitMin)){
            step = pos-conf->uimVar.CanForBackLimitMin;
        }
    }else{
        //DOWN
        if((conf->uimVar.CanForBackLimitMax>0)
            &&(pos>=conf->uimVar.CanForBackLimitMax)){
            return;
        }
        if(step>(conf->uimVar.CanForBackLimitMax-pos)){
            step = conf->uimVar.CanForBackLimitMax - pos;
        }
    }
    if(mode){
        //持续移动
        rate = direction*conf->uimVar.CanForBackRate;
        uim->canForBackEna();
        uim->canSetSTP(uim->getForBackId(),0);
        uim->canSetSPD(uim->getForBackId(),rate);
    }else{
        //相对位置
        uim->canForBackEna();
        uim->canSetSTP(uim->getForBackId(),direction*step);
        uim->canSetSPD(uim->getForBackId(),conf->uimVar.CanForBackRate);
    }
    uim->setCanForBackMove(direction);
    retInfo(QString("CanMove[%0] %1 %2 %3").arg(msg).arg(mode).arg(step).arg(rate));
    if(mode&&!isLimit){
        //电机移动定时器
        canMoveTimer->start(conf->uimVar.CanTimer);
        curCanMoveTimer = QDateTime::currentMSecsSinceEpoch();
    }
    // showBottomUimForBackPos();
}

void MainWindowUI::canStopMove(void){
    retInfo(QString("canStopMove"));
    isKeyCanMove=false;
    //ForBack
    if(!uim->isForBackId())return;
    curForbackReset = 0;
    if(conf->uimVar.CanForBackOff){
        uim->canForBackOff(true);
    }else{
        uim->canSetSPD(uim->getForBackId(),0);
    }
    uim->setCanForBackMove(0);
    // }
    //定时停止
    canMoveTimer->stop();
    // showBottomUimForBackPos();
}

bool MainWindowUI::isStepMax(int code){
    return (code == 518);
}
bool MainWindowUI::isStepOrg(int code){
    return (code == 118);
}
bool MainWindowUI::isStepMove(int code){
    return (code == 117 || code == 245);
}
bool MainWindowUI::isSenseS1(int code){
    return code == 241 || code == 113;
}
bool MainWindowUI::isSenseS2(int code){
    return code == 242 || code == 114;
}
bool MainWindowUI::isSenseS3(int code){
    return code == 243 || code == 115;
}

//判断s1,s2进出
int MainWindowUI::getSense2(int code,int s1,int s2){
    // s1:1IN,3OUT;s2:2IN,4OUT
    //113-S1下降，241-S1上升；114-S2下降，242-S2上升
    if(s1 == 0 && isSenseS1(code)){
        return 0;
    }
    if(s2 == 0 && isSenseS2(code)){
        return 0;
    }
    if((s1==1 && code == 241) || (s1 == 2 && code ==113))
    {//S1
        return 1;
    }
    if((s1==1 && code == 113) || (s1 == 2 && code == 241))
    {//S1
        return 3;
    }
    if((s2==1 && code == 242) || (s2 == 2 && code == 114))
    {//S2
        return 2;
    }
    if((s2==1 && code == 114) || (s2 == 2 && code == 242))
    {//S2
        return 4;
    }
    return 0;
}
//判断s1,s2,s3进出
// int MainWindowUI::getSense3(int code,int s1,int s2,int s3){
//     // s1:1IN,4OUT;s2:2IN,5OUT;s3:3IN,6OUT
//     //113-S1下降，241-S1上升；114-S2下降，242-S2上升
//     if(s1 == 0 && isSenseS1(code)){
//         return 0;
//     }
//     if(s2 == 0 && isSenseS2(code)){
//         return 0;
//     }
//     if(s3 == 0 && isSenseS3(code)){
//         return 0;
//     }
//     if((s1==1 && code == 241) || (s1 == 2 && code ==113))
//     {//S1-
//         return 1;
//     }
//     if((s1==1 && code == 113) || (s1 == 2 && code == 241))
//     {//S1
//         return 4;
//     }
//     if((s2==1 && code == 242) || (s2 == 2 && code == 114))
//     {//S2
//         return 2;
//     }
//     if((s2==1 && code == 114) || (s2 == 2 && code == 242))
//     {//S2
//         return 5;
//     }
//     if((s3==1 && code == 243) || (s3 == 2 && code == 115))
//     {//S3
//         return 3;
//     }
//     if((s3==1 && code == 115) || (s3 == 2 && code == 243))
//     {//S3
//         return 6;
//     }
//     return 0;
// }

//处理限位-卷片前后
void MainWindowUI::procCanForBackSense(int code, QString ts)
{
    int node = uim->getForBackId();
    if(isStepMove(code)){
        //移动通知
        retInfo(QString("%0 ForBackMove %1 %2 %3 %4 %5").arg(ts)
                    .arg(uim->getForBackStep())
                    .arg(uim->getCanForBackMove())
                    .arg(uim->getCanForBack1())
                    .arg(uim->getCanForBack2())
                    .arg(uim->getCanForBackRevStep()));
        if(curForbackReset){
            return;
        }
        if(uim->getForBackStep()!=0){
            uim->setCanForBackMove(0);
        }
        // ui->pushButtonForBackSet->setEnabled(uim->isCanSet(fsUim->getForBackId()));
        if(uim->getCanForBack1()){
            //停止运动.
            if(uim->getForBackStep()==0){
                int step = uim->getCanForBackRevStep();
                if(step != 0){
                    //反向运动
                    uim->canForBackEna();
                    uim->canSetSPD(node,conf->uimVar.CanForBackRate);
                    uim->canSetSTP(node,step);
                }
            }else{
                uim->setCanForBackRevStep(0);
                uim->setCanForBack1(false);
                //设置停止位置
                // saveSetupConfFar(node);
            }
        }
        if(uim->getCanForBack2()){
            //停止运动.
            if(uim->getForBackStep()==0){
                int step = uim->getCanForBackRevStep();
                if(step != 0){
                    //反向运动
                    uim->canForBackEna();
                    uim->canSetSPD(node,conf->uimVar.CanForBackRate);
                    uim->canSetSTP(node,step);
                }
            }else{
                uim->setCanForBackRevStep(0);
                retInfo(QString("%0 set org").arg(ts));
                uim->setCanForBack2(false);
                //设置原点
                // fsUim->canSetOrg(node);
                // saveSetupConfOrg(node);
            }
        }
        if(conf->uimVar.CanForBackOff){
            uim->canForBackOff(true);
        }
        // showBottomUimForBackPos();
        return;
    }
    //停止
    uim->canStop(node);
    if(!curForbackReset){
        return;
    }
    int type = getSense2(code,
                         conf->uimVar.CanForBackSense1,
                         conf->uimVar.CanForBackSense2);
    retInfo(QString("%0=%1 ForBackType=%2 s1=%3 s2=%4 rev=%5").arg(ts)
                .arg(code).arg(type)
                .arg(conf->uimVar.CanForBackSense1)
                .arg(conf->uimVar.CanForBackSense2)
                .arg(uim->getCanForBackRevStep()));
    if(type==0){
        return;
    }
    switch(type){
    case 1:
        //IN-1
        uim->setCanForBack1(true);
        uim->canForBackEna(true);
        uim->canSetSTP(node,conf->uimVar.CanForBackResetStep);
        uim->canSetSPD(node,conf->uimVar.CanForBackResetRate);
        retInfo(QString("%0 1 in ：rev stp").arg(ts));
        curForbackReset = 2;
        break;
    case 2:
        //IN-2
        uim->setCanForBack2(true);
        uim->canForBackEna(true);
        uim->canSetSTP(node,conf->uimVar.CanForBackResetStep);
        uim->canSetSPD(node,conf->uimVar.CanForBackResetRate);
        retInfo(QString("%0 2 in ：rev stp").arg(ts));
        curForbackReset = 2;
        break;
    case 3:
        //OUT-1
        retInfo(QString("%0 1 out org").arg(ts));
        uim->setCanForBack1(false);
        //设置原点
        uim->canSetOrg(node);
        curForbackReset = 0;
        break;
    case 4:
        //OUT-2
        retInfo(QString("%0 2 out").arg(ts));
        uim->setCanForBack2(false);
        // uim->canSetOrg(node);
        curForbackReset = 0;
        break;
    }
    // showBottomUimForBackPos();
}

//处理限位-压片
void MainWindowUI::procCanPushSense(int code, QString ts)
{
    int node = uim->getPushId();
    if(isStepMove(code)){
        //移动通知
        retInfo(QString("%0 PushMove %1 %2 %3 %4 %5").arg(ts)
                    .arg(uim->getPushStep())
                    .arg(uim->getCanPushMove())
                    .arg(uim->getCanPush1())
                    .arg(uim->getCanPush2())
                    .arg(uim->getCanPushRevStep()));
        if(curPushReset){
            return;
        }
        if(uim->getPushStep()!=0){
            uim->setCanPushMove(0);
        }
        // ui->pushButtonPushSet->setEnabled(uim->isCanSet(fsUim->getPushId()));
        if(uim->getCanPush1()){
            //停止运动.
            if(uim->getPushStep()==0){
                int step = uim->getCanPushRevStep();
                if(step != 0){
                    //反向运动
                    uim->canPushEna();
                    uim->canSetSPD(node,conf->uimVar.CanPushRate);
                    uim->canSetSTP(node,step);
                }
            }else{
                uim->setCanPushRevStep(0);
                uim->setCanPush1(false);
                //设置停止位置
                // saveSetupConfFar(node);
            }
        }
        if(uim->getCanPush2()){
            //停止运动.
            if(uim->getPushStep()==0){
                int step = uim->getCanPushRevStep();
                if(step != 0){
                    //反向运动
                    uim->canPushEna();
                    uim->canSetSPD(node,conf->uimVar.CanPushRate);
                    uim->canSetSTP(node,step);
                }
            }else{
                uim->setCanPushRevStep(0);
                retInfo(QString("%0 set org").arg(ts));
                uim->setCanPush2(false);
                //设置原点
                // fsUim->canSetOrg(node);
                // saveSetupConfOrg(node);
            }
        }
        if(conf->uimVar.CanPushOff){
            uim->canPushOff(true);
        }
        // showBottomUimPushPos();
        return;
    }
    //停止
    uim->canStop(node);
    if(!curPushReset){
        return;
    }
    int type = getSense2(code,
                         conf->uimVar.CanPushSense1,
                         conf->uimVar.CanPushSense2);
    retInfo(QString("%0=%1 ForBackType=%2 s1=%3 s2=%4 rev=%5").arg(ts)
                .arg(code).arg(type)
                .arg(conf->uimVar.CanPushSense1)
                .arg(conf->uimVar.CanPushSense2)
                .arg(uim->getCanPushRevStep()));
    if(type==0){
        return;
    }
    switch(type){
    case 1:
        //IN-1
        uim->setCanPush1(true);
        uim->canPushEna(true);
        uim->canSetSTP(node,conf->uimVar.CanPushResetStep);
        uim->canSetSPD(node,conf->uimVar.CanPushResetRate);
        retInfo(QString("%0 1 in ：rev stp").arg(ts));
        curPushReset = 2;
        break;
    case 2:
        //IN-2
        uim->setCanPush2(true);
        uim->canPushEna(true);
        uim->canSetSTP(node,conf->uimVar.CanPushResetStep);
        uim->canSetSPD(node,conf->uimVar.CanPushResetRate);
        retInfo(QString("%0 2 in ：rev stp").arg(ts));
        curPushReset = 2;
        break;
    case 3:
        //OUT-1
        retInfo(QString("%0 1 out org").arg(ts));
        uim->setCanPush1(false);
        //设置原点
        uim->canSetOrg(node);
        curPushReset = 0;
        break;
    case 4:
        //OUT-2
        retInfo(QString("%0 1 out org").arg(ts));
        uim->setCanPush1(false);
        //设置原点
        // uim->canSetOrg(node);
        curPushReset = 0;
        break;
    }
    // showBottomUimPushPos();
}

